﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace U01_boke
{
    /// <summary>
    /// 序列化工厂
    /// </summary>
    public class SerializerFactory
    {
        private static JavaScriptSerializer serializer
        {
            get
            {
                return new JavaScriptSerializer();
            }
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(object obj)
        {
            if (obj == null) return null;
            return serializer.Serialize(obj);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string json) where T : class
        {
            try
            {
                return serializer.Deserialize<T>(json);
            }
            catch
            {
                return default(T);
            }
        }
        public static object ConvertToType(object obj, Type type)
        {
            try
            {
                return serializer.ConvertToType(obj, type);
            }
            catch
            {
                return null;
            }
        }
        public static T ConvertToType<T>(object obj)
        {
            try
            {
                return serializer.ConvertToType<T>(obj);
            }
            catch
            {
                return default(T);
            }
        }


        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static IEnumerable<T> DeserializeToList<T>(string json) where T : class
        {
            try
            {
                return serializer.Deserialize<IEnumerable<T>>(json);
            }
            catch
            {
                return default(IEnumerable<T>);
            }

        }
    }

    /// <summary>
    /// 序列化工厂（Newtonsoft）
    /// </summary>
    public class NewtonsoftFactory
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Serialize(object obj)
        {
            if (null == obj) return null;
            return JsonConvert.SerializeObject(obj);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string json) where T : class
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(json);
            }
            catch (Exception)
            {
                return default(T);
            }
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static IEnumerable<T> DeserializeToList<T>(string json) where T : class
        {
            try
            {
                return JsonConvert.DeserializeObject<IEnumerable<T>>(json);
            }
            catch
            {
                return default(IEnumerable<T>);
            }
        }

    }
}